package com.example.lotterysystem.controller;


import com.example.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import com.example.lotterysystem.common.exception.ControllerException;
import com.example.lotterysystem.common.pojo.CommonResult;
import com.example.lotterysystem.common.utils.JacksonUtil;
import com.example.lotterysystem.controller.param.ShortMessageLoginParam;
import com.example.lotterysystem.controller.param.UserPasswordLoginParam;
import com.example.lotterysystem.controller.param.UserRegisterParam;
import com.example.lotterysystem.controller.result.BaseUserInfoResult;
import com.example.lotterysystem.controller.result.UserLoginResult;
import com.example.lotterysystem.controller.result.UserRegisterResult;
import com.example.lotterysystem.service.UserService;
import com.example.lotterysystem.service.VerificationCodeeService;
import com.example.lotterysystem.service.dto.UserDTO;
import com.example.lotterysystem.service.dto.UserLoginDTO;
import com.example.lotterysystem.service.dto.UserRegisterDTO;

import com.example.lotterysystem.service.enums.UserIdentityEnum;
import com.fasterxml.jackson.databind.ser.Serializers;
import jakarta.annotation.Resource;
import org.apache.catalina.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/user")
public class UserController {

    private final static Logger logger= LoggerFactory.getLogger(UserController.class);
    @Resource(name = "userServiceImpl")
    private  UserService userService;

    @Autowired
    private VerificationCodeeService verificationCodeeService;

    /**
     * 用户注册接口
     * @param param
     * @return
     */

    @RequestMapping("/register")
    public CommonResult<UserRegisterResult>register(@Validated @RequestBody UserRegisterParam param){
        //日志打印
logger.info("用户注册接口被调用，参数为：{}", JacksonUtil.writeValueAsString(param));
        UserRegisterDTO register = userService.register(param);
return CommonResult.success(converToUserRegisterResult(register));
    }

    private UserRegisterResult converToUserRegisterResult(UserRegisterDTO register) {
        UserRegisterResult result=new UserRegisterResult();
        if(register==null){
            throw new ControllerException(ControllerErrorCodeConstants.REGISTER_ERROR);
        }

        result.setUserId(register.getUserId());
        return result;
    }

    /**
     * 发送验证码
     * @param phoneNumber
     * @return
     */
    @RequestMapping("/verificationCode/send")
    public CommonResult<Boolean> sendVerificationCode(String phoneNumber){
        logger.info("sendVerificationCode phoneNumber: {}",phoneNumber);
        verificationCodeeService.sendVerificationCode(phoneNumber);
        return CommonResult.success(Boolean.TRUE);
    }


    /**
     * 用户密码登录
     * @param param
     * @return
     */
    @RequestMapping("/password/login")
    public CommonResult<UserLoginResult> UserPasswordLogin(@Validated @RequestBody UserPasswordLoginParam param){
logger.info("用户密码登录接口被调用，参数为：{}", JacksonUtil.writeValueAsString(param));
        UserLoginDTO userLoginDTO=userService.login(param);
        System.out.println("登录用户信息："+userLoginDTO);
return CommonResult.success(converToUserLoginResult(userLoginDTO));
    }

    private UserLoginResult converToUserLoginResult(UserLoginDTO userLoginDTO) {
        if (userLoginDTO == null)
            throw new ControllerException(ControllerErrorCodeConstants.LOGIN_ERROR);
        return new UserLoginResult(userLoginDTO.getToken(),userLoginDTO.getIdentity().name());
    }


    /**
     * 用户短信登录
     * @param param
     * @return
     */
    @RequestMapping("/message/login")
    public CommonResult<UserLoginResult> UserPasswordLogin(@Validated @RequestBody ShortMessageLoginParam param){
logger.info("用户短信登录接口被调用，参数为：{}", JacksonUtil.writeValueAsString(param));
        UserLoginDTO userLoginDTO=userService.login(param);
        return CommonResult.success(converToUserLoginResult(userLoginDTO));
    }


    /**
     * 获取人员列表
     */
    @RequestMapping("/base-user/find-list")
    public CommonResult<List<BaseUserInfoResult>> findBaseUserInfo(String identity){
        logger.info("获取人员列表接口被调用，参数为：{}", identity);
        List<UserDTO>userDTOList=userService.findBaseUserInfo(UserIdentityEnum.fromIdentity(identity));
        return CommonResult.success(converToBaseUserInfoResult(userDTOList));
    }

    private List<BaseUserInfoResult> converToBaseUserInfoResult(List<UserDTO> userDTOList) {
if(CollectionUtils.isEmpty(userDTOList)){
    return Arrays.asList();
}

return userDTOList.stream().map(userDTO -> {
    BaseUserInfoResult result=new BaseUserInfoResult();
        result.setUserId(userDTO.getUserId());
        result.setUserName(userDTO.getUserName());
        result.setIdentity(userDTO.getIdentity().name());
        return result;
}).collect(Collectors.toList());
    }
}
